package com.xinxing.learning.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.xinxing.learning.datasource.model.DynamicDataSourceModel;
import com.xinxing.learning.datasource.util.DynamicDBUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;

@Slf4j
public class DynamicDataSourceFactory {

    public static DruidDataSource buildDruidDataSource(DataSourceProperties properties) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getUrl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());

        // druidDataSource.setInitialSize(properties.getInitialSize());
        // druidDataSource.setMaxActive(properties.getMaxActive());
        // druidDataSource.setMinIdle(properties.getMinIdle());
        // druidDataSource.setMaxWait(properties.getMaxWait());
        // druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        // druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        // druidDataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis());
        // druidDataSource.setValidationQuery(properties.getValidationQuery());
        // druidDataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout());
        // druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
        // druidDataSource.setTestOnReturn(properties.isTestOnReturn());
        // druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        // druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
        // druidDataSource.setSharePreparedStatements(properties.isSharePreparedStatements());

        try {
            // druidDataSource.setFilters(properties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return druidDataSource;
    }
}
